#!/bin/bash

set -e

export RUBY_VERSION=$1

docker_compose="docker compose --file test-envs/docker-compose-$2.yml"

function cleanup() {
  $docker_compose down
}

function die() {
    echo $@
    exit 1
}

trap cleanup EXIT

export RAILS_ENV=test
export DATABASE_NAME=tracks_test

$docker_compose build
$docker_compose up -d
script/poll-for-db $2

# Leaving this in since it will be needed for Rails 5
$docker_compose run web bin/rails db:environment:set RAILS_ENV=test || true

if [ "$2" == "mysql" ];
then
  $docker_compose run web bin/rake db:reset
else
  $docker_compose run web bin/rake db:migrate
  $docker_compose run web bin/rake db:seed
fi
echo "==> Running the tests…"
$docker_compose run web bin/rails test
